<?php
namespace hotel\modules\apiadmin\controllers;
use hotel\common\models\Hotel;
use hotel\common\models\Promise;
use hotel\common\utils\HotelServices;
use hotel\common\utils\Tool;
use hotel\common\utils\Zipfile;
use hotel\modules\apiadmin\models\HotelFacilitiesType;
use \Yii;

class HotelController extends CoreController
{
	//获取酒店信息
	public function actionGetHotelData(){
		$data = Hotel::find()->where("appid = '$this->appid'")->asArray()->one();
		$arr = [];
		if(!empty($data['hotel_facilities'])){
			$hotel_facilities = explode(',',$data['hotel_facilities']);
			foreach($hotel_facilities as $k=>$v){
				$res = HotelFacilitiesType::find()->where("id = $v")->asArray()->one();
				array_push($arr,$res);
			}
		}
		$data['hotel_facilities_data'] = $arr;
		return Tool::showRes([0, '获取酒店信息成功'],$data);
	}

	//编辑酒店信息
	public function actionEditHotelData(){
		$id = $this->request('id');
		if(empty($id)){
			return Tool::showRes([400, 'id不能为空']);
		}
		$model = new Hotel();
		$model = $model::findOne($id);
		$model->name = $this->request('name');
		$model->cover_image_url = $this->request('cover_image_url');
		$model->traffic_information = $this->request('traffic_information');
		$model->banners = $this->request('banners');
		$model->score = $this->request('score');
		$model->city = $this->request('city');
		$model->phone = $this->request('phone');
		$model->starting_price = $this->request('starting_price');
		$model->address = $this->request('address');
		$model->position = $this->request('position');
		$model->desc = $this->request('desc');
		$model->reservation_notice = $this->request('reservation_notice');
		$model->hotel_facilities = $this->request('hotel_facilities');
		$model->video = $this->request('video');
		$model->video_image_url = $this->request('video_image_url');
		$model->buy_peoples = $this->request('buy_peoples');
		$model->must_read = $this->request('must_read');

		if($model->save()) {
			return Tool::showRes([0, '操作成功']);
		}
		return Tool::showRes([400, '操作失败']);
	}


	//承诺书列表
	public function actionGetPromiseData(){
		$post_data = Yii::$app->request->post();
		$query = Promise::find();
		$page_size = isset($post_data['page_size']) ? $post_data['page_size'] : 10;
		$page = isset($post_data['page']) ? $post_data['page'] : 1;
		$phone = $this->request('phone');

		if (!empty($phone)) {
			$like_conditon = ['or',['like','phone',$phone],['like','id_card',$phone],['like','name',$phone]];
			$query->andwhere($like_conditon);
		}
		$start_date = $post_data['start_date'];
		$end_date= $post_data['end_date'];
		if(!empty($start_date) and !empty($end_date)){
			$query->andWhere(['between','date',$start_date, $end_date]);
		}

		$query->andWhere("appid = '$this->appid'");
		$list = $query->offset($page_size*($page-1))->limit($page_size)->orderBy('id desc')->asArray()->all();
		$counts = $query->count();
		$data['list'] = $list;
		$data['pageInfo'] = [
			'count' => $counts,
			'pageSize' => $page_size,
			'currPage' => $page,
			'tatalPage' => ceil($counts/$page_size),
		];
		return Tool::showRes([0, '获取数据成功'],$data);
	}






	public function actionDownloadImage()
	{
		$get_data = Yii::$app->request->get();
		$query = Promise::find();
		$phone = $this->request('phone');
		if(!empty($phone)){
			$query->andWhere("phone = $phone");
		}
		$appid = $get_data['appid'];
		$start_date = $get_data['start_date'];
		$end_date= $get_data['end_date'];
		if(!empty($start_date) and !empty($end_date)){
			$query->andWhere(['between','date',$start_date, $end_date]);
		}

		$query->andWhere("appid = '$appid'");
		$list = $query->asArray()->all();

		if (empty($list)){
			return false;
		}

		$dfile =  tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
		$zip = new Zipfile();
		//----------------------
		$filename = 'image.zip'; //下载的默认文件名

		//以下是需要下载的图片数组信息，将需要下载的图片信息转化为类似即可

		foreach($list as $v){
			$zip->add_file(file_get_contents($v['img_url']),  $v['date'].'-'.$v['room_num'].'.jpg');
			// 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称, 可包含路径
			// 或是想打包整个目录 用 $zip->add_path($image_path);
		}
		//----------------------
		$zip->output($dfile);

		// 下载文件
		ob_clean();
		header('Pragma: public');
		header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
		header('Cache-Control:no-store, no-cache, must-revalidate');
		header('Cache-Control:pre-check=0, post-check=0, max-age=0');
		header('Content-Transfer-Encoding:binary');
		header('Content-Encoding:none');
		header('Content-type:multipart/form-data');
		header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
		header('Content-length:'. filesize($dfile));
		$fp = fopen($dfile, 'r');
		while(connection_status() == 0 && $buf = @fread($fp, 8192)){
			echo $buf;
		}
		fclose($fp);
		@unlink($dfile);
		@flush();
		@ob_flush();
		exit();
	}
}